// index.js
Page({
  data: {
    // 表单数据
    startDate: '',
    endDate: '',
    detectType: 0, // 0-识别到人员, 1-未识别到人员
    notifyTime: 'realtime',
    notifyTimeText: '实时通知',
    scheduledTime: '',
    
    // 通知对象数据
    residentInfo: {
      name: '张先生',
      phone: '138****8888'
    },
    propertyManagers: [
      { name: '物业客服', phone: '180****6666' }
    ],
    phoneNumbers: ['188****9999'],
    
    // UI状态
    showDatePicker: false,
    datePickerType: 'start', // start/end
    minDate: '2023-01-01',
    maxDate: '2030-12-31',
    currentDate: '',
    
    showNotifyTimePicker: false,
    showTimePicker: false,
    currentTime: ''
  },

  // 页面加载
  onLoad(options) {
    if (options.id) {
      this.loadConfigData(options.id);
    }
  },

  // 加载配置数据（模拟）
  loadConfigData(id) {
    console.log('加载配置ID:', id);
    // 实际应用中从服务器加载数据
    setTimeout(() => {
      this.setData({
        startDate: '2023-06-01',
        endDate: '2023-06-30',
        detectType: 1
      });
    }, 500);
  },

  // 返回上一页
  onBack() {
    wx.navigateBack();
  },

  // 打开开始日期选择器
  openStartDatePicker() {
    this.setData({
      showDatePicker: true,
      datePickerType: 'start',
      currentDate: this.data.startDate || this.formatDate(new Date())
    });
  },

  // 打开结束日期选择器
  openEndDatePicker() {
    this.setData({
      showDatePicker: true,
      datePickerType: 'end',
      currentDate: this.data.endDate || this.formatDate(new Date())
    });
  },

  // 关闭日期选择器
  closeDatePicker() {
    this.setData({ showDatePicker: false });
  },

  // 日期改变处理
  onDateConfirm(e) {
    const selectedDate = e.detail.value;
    this.setData({
      currentDate: selectedDate
    });
  },
  
  // 确认日期选择
  confirmDate() {
    const { currentDate, datePickerType } = this.data;
    if (datePickerType === 'start') {
      this.setData({
        startDate: currentDate,
        showDatePicker: false
      });
    } else {
      this.setData({
        endDate: currentDate,
        showDatePicker: false
      });
    }
  },

  // 日期格式化
  formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  },

  // 设置识别类型
  setDetectType(e) {
    const type = parseInt(e.currentTarget.dataset.type);
    this.setData({ detectType: type });
  },

  // 打开通知时间选择器
  openNotifyTimePicker() {
    this.setData({ showNotifyTimePicker: true });
  },

  // 关闭通知时间选择器
  closeNotifyTimePicker() {
    this.setData({ showNotifyTimePicker: false });
  },

  // 选择通知时间类型
  selectNotifyTime(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      notifyTime: type,
      notifyTimeText: type === 'realtime' ? '实时通知' : '定时通知'
    });
    
    if (type === 'scheduled') {
      setTimeout(() => {
        this.openTimePicker();
      }, 200);
    }
  },

  // 打开时间选择器
  openTimePicker() {
    this.setData({
      showTimePicker: true,
      showNotifyTimePicker: false,
      currentTime: this.data.scheduledTime || '08:00'
    });
  },

  // 关闭时间选择器
  closeTimePicker() {
    this.setData({ showTimePicker: false });
  },

  // 时间改变处理
  onTimeConfirm(e) {
    this.setData({
      currentTime: e.detail.value
    });
  },
  
  // 确认时间选择
  confirmTime() {
    this.setData({
      scheduledTime: this.data.currentTime,
      showTimePicker: false,
      notifyTimeText: `定时通知 ${this.data.currentTime}`
    });
  },

  // 添加物业管理员
  addPropertyManager() {
    const newManagers = this.data.propertyManagers;
    newManagers.push({
      name: '新管理员',
      phone: '请设置号码'
    });
    this.setData({
      propertyManagers: newManagers
    });
  },

  // 移除物业管理员
  removePropertyManager(e) {
    const index = e.currentTarget.dataset.index;
    const newManagers = this.data.propertyManagers;
    newManagers.splice(index, 1);
    this.setData({
      propertyManagers: newManagers
    });
  },

  // 添加手机号
  addPhoneNumber() {
    const newNumbers = this.data.phoneNumbers;
    newNumbers.push('');
    this.setData({
      phoneNumbers: newNumbers
    });
  },

  // 更新手机号
  updatePhoneNumber(e) {
    const index = e.currentTarget.dataset.index;
    const value = e.detail.value;
    const newNumbers = this.data.phoneNumbers;
    newNumbers[index] = value;
    this.setData({
      phoneNumbers: newNumbers
    });
  },

  // 移除手机号
  removePhoneNumber(e) {
    const index = e.currentTarget.dataset.index;
    const newNumbers = this.data.phoneNumbers;
    newNumbers.splice(index, 1);
    this.setData({
      phoneNumbers: newNumbers
    });
  },

  // 阻止事件冒泡
  stopPropagation(e) {
    return;
  },

  // 表单提交
  onSubmit() {
    const { startDate, endDate, phoneNumbers } = this.data;
    
    // 表单验证
    if (!startDate) {
      wx.showToast({ title: '请选择开始日期', icon: 'none' });
      return;
    }
    
    if (!endDate) {
      wx.showToast({ title: '请选择结束日期', icon: 'none' });
      return;
    }
    
    // 验证手机号 - 使用for循环替代for...of
    for (let i = 0; i < phoneNumbers.length; i++) {
      const phone = phoneNumbers[i];
      if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
        wx.showToast({ title: '手机号格式不正确', icon: 'none' });
        return;
      }
    }
    
    wx.showLoading({ title: '保存中...' });
    
    // 模拟保存操作
    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({ title: '保存成功' });
      wx.navigateBack();
    }, 1000);
  }
});